package com.cjx.stars.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjx.stars.mapper.UserTeamMapper;
import com.cjx.stars.pojo.table.UserTeam;
import com.cjx.stars.service.UserTeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author cjx
* @description 针对表【user_team(用户队伍关系)】的数据库操作Service实现
* @createDate 2024-03-25 21:34:03
*/
@Service
public class UserTeamServiceImpl extends ServiceImpl<UserTeamMapper, UserTeam>
    implements UserTeamService {

    @Resource
    UserTeamMapper userTeamMapper;

    @Override
    public List<Long> getUserIdsByTeamId(Long teamId) {
        LambdaQueryWrapper<UserTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserTeam::getTeamId,teamId).select(UserTeam::getUserId);
        List<Object> list = userTeamMapper.selectObjs(wrapper);
        return list.stream().map(o -> (Long) o).collect(Collectors.toList());
    }

    @Override
    public boolean[] isJoin(String userId, List<Long> teamIds) {
        int size = teamIds.size();
        boolean[] booleans = new boolean[size];
        for (int i = 0; i < size; i++) {
            long count = count(new LambdaQueryWrapper<UserTeam>().eq(UserTeam::getUserId, userId).eq(UserTeam::getTeamId, teamIds.get(i)));
            //代表该用户加入了该队伍
            if (count > 0 ){
                booleans[i] = true;
            }
        }
        return booleans;
    }

    @Override
    public int getTeamCount(Long teamId) {
        return (int) count(new LambdaQueryWrapper<UserTeam>().eq(UserTeam::getTeamId,teamId));
    }

    @Override
    public void removeByTeamIdAndUserId(Long teamId, Long userId) {
        remove(new LambdaQueryWrapper<UserTeam>().eq(UserTeam::getTeamId,teamId).eq(UserTeam::getUserId,userId));
    }
}




